package postgres

import (
	"context"
	"gorm.io/gorm"
	"manager/errors"
	"manager/model"
	"manager/model/entity"
	"manager/utils"
)

var Alarm = &alarm{}

type alarm struct{}

// Create 创建
func (a *alarm) Create(ctx context.Context, m *entity.Alarm) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *alarm) Find(ctx context.Context, in *model.AlarmInfoRequest) (*entity.Alarm, error) {
	e := &entity.Alarm{}

	q := GetDB(ctx).Model(&entity.Alarm{})

	if in.Id > 0 {
		err := q.First(&e, in.Id).Error
		return e, err
	}

	count := 0

	if in.UserId != nil {

		q = q.Where("user_id = ?", in.UserId)

		count++
	}

	if in.Type != nil {

		q = q.Where("type = ?", in.Type)

		count++
	}

	if in.EbikeId != nil {

		q = q.Where("ebike_id = ?", in.EbikeId)

		count++
	}

	if in.EbikeTypeId != nil {

		q = q.Where("ebike_type_id = ?", in.EbikeTypeId)

		count++
	}

	if in.EbikeTypeName != nil {

		q = q.Where("ebike_type_name like ?", in.EbikeTypeName)

		count++
	}

	if in.Level != nil {

		q = q.Where("level = ?", in.Level)

		count++
	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

		count++
	}

	if count == 0 {
		return e, errors.New("condition illegal")
	}

	err := q.First(&e).Error
	return e, err
}

// Update 更新
func (a *alarm) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	return GetDB(ctx).Model(&entity.Alarm{}).Where("id = ?", id).Updates(dict).Error
}

// Delete 删除
func (a *alarm) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.Alarm{}, id).Error
}

// List 列表查询
func (a *alarm) List(ctx context.Context, in *model.AlarmListRequest) (int, []*entity.Alarm, error) {
	var (
		q      = GetDB(ctx).Model(&entity.Alarm{})
		err    error
		total  int64
		alarms []*entity.Alarm
	)

	if in.UserId != nil {

		q = q.Where("user_id = ?", in.UserId)

	}

	if in.Type != nil {

		q = q.Where("type = ?", in.Type)

	}

	if in.EbikeId != nil {

		q = q.Where("ebike_id = ?", in.EbikeId)

	}

	if in.EbikeTypeId != nil {

		q = q.Where("ebike_type_id = ?", in.EbikeTypeId)

	}

	if in.EbikeTypeName != nil {

		q = q.Where("ebike_type_name like ?", in.EbikeTypeName)

	}

	if in.Level != nil {

		q = q.Where("level = ?", in.Level)

	}

	if in.FrameCode != nil {

		q = q.Where("frame_code like ?", utils.SqlLike(in.FrameCode))

	}

	if len(in.TimeZone) == 2 {
		q = q.Where("created_at between ? and ?", in.TimeZone[0], in.TimeZone[1])
	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&alarms).Error; err != nil {
		return 0, nil, err
	}
	return int(total), alarms, nil
}

// ExecTransaction db事务执行
func (a *alarm) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
